package com.lgmshare.k3.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Rect;
import android.util.AttributeSet;

import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;

import com.lgmshare.k3.R;

public class ImageViewWithBorder extends AppCompatImageView {

    private Paint mPaint = new Paint();
    private Rect mRect;
    private int mWidth = 1;

    public ImageViewWithBorder(Context context) {
        this(context, null);
    }

    public ImageViewWithBorder(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ImageViewWithBorder(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mPaint.setStrokeWidth(0.0F);
        this.mPaint.setColor(ContextCompat.getColor(context, R.color.red));
        this.mPaint.setStyle(Style.STROKE);
        this.mRect = new Rect();
    }

    public void setBorderWidth(int width) {
        mWidth = width;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();
        canvas.clipRect(this.mRect);
        super.onDraw(canvas);
        canvas.restore();
        canvas.drawRect(this.mRect, this.mPaint);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        getDrawingRect(this.mRect);
        mRect.inset(mWidth, mWidth);
    }
}